package com.fjwt.gz.mch.ctrl.fansAward;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.FansAwardDTO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.FansAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/fans/award")
public class FansAwardController extends CommonCtrl {
    @Autowired
    private FansAwardService fansAwardService;


    /**
     * 获取卡券激活提示语
     */
    @PostMapping("/activate/alertMsg")
    public ApiRes alertMessage() {
        FansAwardDTO fansAwardDTO = getObject(FansAwardDTO.class);
        fansAwardDTO.setAppId(getCurrentUser().getAppId());
        log.info("卡券提示语调用：请求地址：/api/fans/award/activate/alertMsg，请求参数：{}", JSONObject.toJSONString(fansAwardDTO));
        String result = fansAwardService.getActivateAlertMsg(fansAwardDTO);
        log.info("卡券提示语调用：请求地址：/api/fans/award/activate/alertMsg，返回结果：{}", result);
        return ApiRes.ok(result);
    }

    /**
     * 卡券激活
     */
    @PostMapping("/activeCoupon")
    public ApiRes activeCoupon() {
        FansAwardDTO fansAwardDTO = getObject(FansAwardDTO.class);
        Long fansId = getCurrentUser().getFansId();
        fansAwardDTO.setFansId(fansId);//激活人ID（客户经理）
        fansAwardDTO.setAppId(getCurrentUser().getAppId());
        log.info("卡券激活接口：/api/fans/award/activeCoupon，请求参数：{}，激活人ID:{}", JSON.toJSONString(fansAwardDTO), fansId);
        fansAwardService.activeCoupon(fansAwardDTO);

        return ApiRes.ok();
    }

    /**
     * 用户实物奖品核销
     *
     * @return
     */
    @PostMapping("/destroyAward")
    @Transactional(rollbackFor = Exception.class)
    public ApiRes destroyAward() {
        FansAwardDTO fansAwardDTO = getObject(FansAwardDTO.class);
        log.info("用户奖品核销接口调用，接口地址：/api/fansAward/destroyAward，请求参数:{}", JSONUtil.toJsonStr(fansAwardDTO));
        fansAwardDTO.setFansId(getCurrentUser().getFansId());
        fansAwardDTO.setAppId(getCurrentUser().getAppId());
        return ApiRes.ok(fansAwardService.destroyAward(fansAwardDTO));
    }

    /**
     * 查询cdk奖品
     **/
    @PostMapping("/getCdk")
    public ApiRes getCdk() {
        Long fansAwardId = getValLong("fansAwardId");
        Long fansId = getCurrentUser().getFansId();
        return ApiRes.ok(fansAwardService.getCdk(fansAwardId,fansId));
    }

}
